package com.cskaoyan.controller;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.service.CouponService;
import com.cskaoyan.service.CouponServiceImpl;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.CouponUserVo;
import com.cskaoyan.vo.CouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class CouponController {
    @Autowired
    CouponService couponService;


    /**
     * 作者：whl
     * 功能：获取优惠券信息【get】
     *
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("admin/coupon/list")//page=1&limit=20&sort=add_time&order=desc
    public BaseRespVo list(Integer page,Integer limit,String sort,String order){
        //在service层做分页和排序
        CouponVo couponVo = couponService.query(page,limit,sort,order);
        //page和limit 分页  -》分页插件
        //sort和order 数据排序 -》逆向工程生成的方法来做
        return BaseRespVo.ok(couponVo);
    }

    /**
     * by：whl【待完善】
     * 详情展示【get】
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("admin/coupon/listuser")//page=1&limit=20&couponId=27&sort=add_time&order=desc
    public BaseRespVo listUser(Integer page,Integer limit,Integer couponId,String sort,String order){
        CouponUserVo couponUserVo = couponService.couponListUser(page,limit,couponId,sort,order);
        return BaseRespVo.ok(couponUserVo);
    }


    /**
     * by:whl
     * 功能：创建优惠券【post】
     * @param coupon
     * @return
     */
    @RequestMapping("admin/coupon/create")
    public BaseRespVo create(@RequestBody Coupon coupon){
        //Coupon coupon1 = new Coupon(null,coupon.getName(),coupon.getDesc(),coupon.getTag(),coupon.getTotal(),coupon.getDiscount(),coupon.getMin(),coupon.getLimit(),coupon.getType(),coupon.getStatus(),coupon.getGoodsType(),coupon.getGoodsValue(),null,coupon.getTimeType(),coupon.getDays(),coupon.getStartTime(),coupon.getEndTime(),new Date(),new Date(),false);
        Coupon data = couponService.couponCreate(coupon);
        return BaseRespVo.ok(data);
    }

    /**
     * by:whl
     * 功能：修改优惠券【post】
     * @param coupon
     * @return
     */
    @RequestMapping("admin/coupon/update")
    public BaseRespVo update(@RequestBody Coupon coupon){
        Coupon data = couponService.couponUpdate(coupon);
        return BaseRespVo.ok(data);
    }

    /**
     * by:whl
     * 功能：根据主键删除优惠券【post】
     * @param coupon
     * @return
     */
    @RequestMapping("admin/coupon/delete")
    public BaseRespVo delete(@RequestBody Coupon coupon){
        couponService.couponDelete(coupon);
        return BaseRespVo.ok();
    }

    /**
     * by:whl
     * 功能：read
     * @param id
     * @return
     */
    @RequestMapping("admin/coupon/read")
    public BaseRespVo read(Integer id){
        Coupon coupon = couponService.selectCouponById(id);
        return BaseRespVo.ok(coupon);
    }
}
